package cn.edu.neu.springbootdemo.action;

import cn.edu.neu.springbootdemo.core.Constants;
import cn.edu.neu.springbootdemo.model.User;
import cn.edu.neu.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")

public class UserAction {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUserList")
    public String getUserList(User user, Map<String,List<User>> m){
        //向数据库查询user信息，存储到List里，再把list放到map里
        //MAPPER定义规则List<User>，在xml里查询
        List<User> userList = userService.getUserList(user);
        System.out.println("userList------------:"+userList);
        m.put("userList",userList);
        System.out.println("m------------:"+m);
        return "user/userList";
    }


    @RequestMapping("/addUser")
    public String addUser(User user, Map<String,String> m){
        int f = userService.addUser(user);
        if (f == 0){
            m.put("result","yes");
            m.put("msg","注册成功");
        }else if(f == 1){
            m.put("result","no");
            m.put("msg","注册不成功，用户名重复");
        }else{
            m.put("result","no");
            m.put("msg","注册不成功，数据库错误");
        }
        return "user/result";
    }
    @ResponseBody
    @RequestMapping("/checkUsername")
    public Map<String,String> checkUsername(User user) {
        System.out.println("checkUsername========================"+user);
        boolean f=userService.checkUsername(user);  //可以注册
        Map<String,String> m=new HashMap<String,String>();
        if(f){
            m.put("result", "yes");
            m.put("msg", "");
        }else {
            m.put("result", "no");
            m.put("msg", "对不起，该用户名已存在，请更换");
        }
        System.out.println("checkUsername:"+m);
        return m;
    }

    //登录功能
    @RequestMapping("/login")
    public String login(User user, HttpSession session, Map<String, String> m){
        //1.查数据库有没有这个人
        User dbUser = userService.existsUser(user);
        //2.如果有，登录信息存起来，跳转到目标页
        if (dbUser != null){
            session.setAttribute(Constants.LOGIN_USER,dbUser);
            m.put("login","yes");
            String redirUrl =  (String) session.getAttribute(Constants.ORIGINAL_URL);
            return "redirect:"+redirUrl;
        }else {
        //3.没有还得回到登录页
            m.put("login",Constants.LOGIN_ERR);
            return "/login";
        }
    }

    //查询用户信息，弹出新页面edit.html
    @RequestMapping("/getUser")
    public String getUser(String userid, Map<String,User> m){
        System.out.println("getUser-------------------------------:");
        User user = userService.getUser(userid);
        m.put("user",user);
        return "/user/edit";
    }
}
